#pragma once
#include "Character/Part.h"
#include "Character/Creature.h"
#include "Common/CharacterDefine.h"
#include <stdint.h>
const uint32_t g_hateDistance = 60;  //单位米
const uint32_t g_IntervalBornFlag = 1000; //单位ms
enum MonsterFightType
{
	ENUM_MONSTER_FIGHT_NONE = 0,				//错误类型
	ENUM_MONSTER_FIGHT_TYPE_NO_ATTACK = 1,		//不攻击 沙包怪
	ENUM_MONSTER_FIGHT_TYPE_ATTACK_AFTER_ATTACK = 2, //受攻击后攻击 被动怪
	ENUM_MONSTER_FIGHT_TYPE_ACTIVE_ATTACK = 3, //主动怪
	ENUM_MONSTER_FIGHT_MAX,
};

enum MonsterType
{
	ENUM_MONSTER_TYPE_NONE = 0,
	ENUM_MONSTER_TYPE_LITTLE_MONSTER = 1, //小怪
	ENUM_MONSTER_TYPE_BIG_MONSTER = 2, //精英
	ENUM_MONSTER_TYPE_BOSS_MONSTER = 3, //boss
	ENUM_MONSTER_TYPE_SPECIAL_SUMMON_FOR_OWNER_USE_SKILL = 4, //特殊召唤物
	ENUM_MONSTER_TYPE_MAX,
};

enum SummonMonsterType
{
	SUMMON_MONSTER_TYPE_CAN_BEATTACK_CAN_MOVE = 1,
	SUMMON_MONSTER_TYPE_CANNOT_BEATTACK_CANNOT_MOVE = 2,
	SUMMON_MONSTER_TYPE_CAN_BEATTACK_CANNOT_MOVE = 3,
	SUMMON_MONSTER_TYPE_CANNOT_BEATTACK_CAN_MOVE = 4,
};

//可触发怪物的子类型
enum TriggerMonsterType
{
	TRIGGLE_MONSTER_TYPE_TARGET_EVERYTHING = 1, //任意物体为目标
	TRIGGLE_MONSTER_TYPE_TARGET_PLAYER = 2,     //角色为目标
	TRIGGLE_MONSTER_TYPE_TARGET_MONSTER = 3,    //怪物为目标
	TRIGGLE_MONSTER_TYPE_TARGET_NPC = 4,  //NPC为目标
};

//陷阱类怪物
enum TrapMonsterType
{
	TRAP_MONSTER_TYPE_WEEKLY_TRAP = 1, //周期类陷阱（如周期性喷火)
	TRAP_MONSTER_TYPE_TRIGGLE_TRAP = 2, //触发类陷阱
	TRAP_MONSTER_TYPE_NOMOVE_TRAP = 3, //恒定陷阱
};

//广义上怪物的大类型
enum ParentMonsterType
{
	PARENT_MONSTER_TYPE_NONE = 0, //NONE
	PARENT_MONSTER_TYPE_MONSTER = 1, //怪物
	PARENT_MONSTER_TYPE_TA = 2,  //塔
	PARENT_MONSTER_TYPE_SUMMON = 3,  //
	PARENT_MONSTER_TYPE_CAN_DESTROYED_THING = 4, //可破坏物件
	PARENT_MONSTER_TYPE_CAN_USE_THING = 5, //可使用物件
	PARENT_MONSTER_TYPE_CAN_TRIGGER_THING = 6, //可触发物件
	PARENT_MONSTER_TYPE_CAN_TRAP = 7,  //陷阱
	PARENT_MONSTER_TYPE_FIGHT_NPC = 8, //战斗NPC
	PARENT_MONSTER_TYPE_MOVE_BIAOCHE = 9, //个人运镖镖车
	PARENT_MONSTER_TYPE_MOVE_BIAOCHE_MONSTER = 10, //攻击运镖镖车的怪物
	PARENT_MONSTER_TYPE_MOVE_TEAM_BIAOCHE = 11, //团队运镖镖车
	PARENT_MONSTER_TYPE_NO_MOVE_BOSS = 12, //不能移动的BOSS, 双头龙
	PARENT_MONSTER_TYPE_SCENE_BUILDING_BOSS = 13, //场景建筑BOSS
	PARENT_MONSTER_TYPE_CAN_USE_DROP = 14, //可采集物件
	PARENT_MONSTER_TYPE_BOSS_ZHONGSHEN = 15, //众神BOSS
	PARENT_MONSTER_TYPE_TRAP_CAN_BE_ATTATED = 16, //陷阱可以被攻击
	PARENT_MONSTER_TYPE_MAX, //max
};

enum MonsterCfgType
{
	ENUM_MONSTER_CFG_TYPE_NONE = 0,
	ENUM_MONSTER_CFG_TYPE_FIELD = 1, //野外怪
	ENUM_MONSTER_CFG_TYPE_COPY_SCENE = 2, //副本
	ENUM_MONSTER_CFG_TYPE_MAX,
};

enum MonsterViewType
{
	ENUM_MONSTER_VIEW_TYPE_NONE = 0,
	ENUM_MONSTER_VIEW_TYPE_CIRCLE = 1, //圆形
	ENUM_MONSTER_VIEW_TYPE_SECTOR = 2, //扇形
	ENUM_MONSTER_VIEW_TYPE_BALL = 3, //球形
	ENUM_MONSTER_VIEW_TYPE_MAX,
};

enum MonsterPatrolType
{
	ENUM_MONSTER_PATROL_TYPE_ROUND_TRIP = 1, //往返巡逻
	ENUM_MONSTER_PATROL_TYPE_LOOP_TRIP = 2, //循环巡逻
	ENUM_MONSTER_PATROL_TYPE_ONE_WAY_TRIP = 3, //一次性巡逻
};

enum MonsterDropOwnerState
{
	MonsterDropOwnerState_None =0,
	MonsterDropOwnerState_Prop =1,	//受保护状态
	MOnsterDropOwnerState_Owner =2,	//有所属状态
};

//怪物创建类型
enum EMonsCreateType
{
	EMonsCreateType_None		= 0,
	EMonsCreateType_Map			= 1,	//地图怪(地图ID)
	EMonsCreateType_Dup			= 2,	//副本怪(副本ID)
	EMonsCreateType_Activity	= 3,	//活动怪(活动ID)
	EMonsCreateType_SummonBuff	= 4,	//召唤buff创建的怪
	EMonsCreateType_SummonSkill = 5,	//召唤技能创建的怪
	EMonsCreateType_Mission		= 6,	//任务怪(任务ID)
	EMonsCreateType_Thief		= 7,	//盗宝贼怪
	EMonsCreateType_Effect		= 8,	//触发器怪
	EMonsCreateType_GM			= 9,	//GM怪
	EMonsCreateType_Plane		= 10,	//位面怪
	EMonsCreateType_Escort		= 11,	//镖车怪
	EMonsCreateType_RobEscort	= 12,	//抢劫镖车怪
	EMonsCreateType_ReliveSkill	= 13,	//复活技能怪
	EMonsCreateType_Limit,
};
//怪物创建参数(记录怪物创建的来源)
struct MonsCreateParam
{
	EMonsCreateType createType;	//怪物创建类型
	uint64_t createTypeVal;		//创建类型值
	MonsCreateParam()
	{
		createType = EMonsCreateType_None;
		createTypeVal = 0;
	}
	void clear()
	{
		createType = EMonsCreateType_None;
		createTypeVal = 0;
	}
};

class Monster : public Creature
{
public:

	enum enTimeID
	{
		enTimeID_PropTIme	=1,
		enTimeID_OnwerTime	=2,
		enTimeID_ExistTime = 3,
		enTimeID_CorpseTime = 4,
		enTimeID_BornFlag = 5,
	};

	Monster();
	virtual ~Monster();
	typedef std::list<CharIDType> CidLst;
public:
	virtual bool Init();
	virtual bool UnInit();
	bool InitAttr(uint32_t monsterId);
	virtual void Update(uint64_t tick);
	virtual void OnDead(CharIDType killerCid);
	virtual void OnCorpse();
	bool	IsInCorpseTime();
	//马上销毁怪物
	void    OnDestroyMonsterNow();

	virtual bool CheckAttrValue(uint32_t CANum, uint32_t attrValue) { return true; }
	virtual bool AddAttr(uint32_t CANum, int64_t attrValue, bool syn, uint64_t relationCid = 0);
	virtual bool AddAttrMA(uint32_t PANum, int64_t attrValue, bool syn, uint64_t relationCid = 0);
	virtual bool AddAttrCache(uint32_t CANum, int64_t attrValue, uint64_t relationCid = 0);
	virtual void SetAttrMA(uint32_t PANum, int64_t attrValue, bool syn = false);
	virtual void SetAttr(uint32_t CANum, int64_t attrValue, bool syn = false);
	virtual void SetAttrCache(uint32_t CANum, int64_t attrValue);

	virtual int64_t  GetAttr(uint32_t CANum) ;
	virtual int64_t GetPAttrValue(uint32_t attrPANum){ return 0; };
	virtual uint32_t GetAttrNum(uint32_t CommonAttrNum);

	virtual uint32_t GetConfigId();

	virtual void OnRevive(uint64_t saverCid);

	bool  AttrsFormula(uint32_t MANum, uint64_t relationCid, bool cache);
	void  SynAttrCache(uint32_t MANum);

	virtual void GetVisibleDataToClient(CreaturesCreateData& cvData);
	virtual void GetMoveDataToClient(ActorMove &mouseMove, ActorRockerMove &rockmove);
	virtual bool Born(CharIDType monsterCid, uint32_t monsterId, uint32_t sceneId, Point3<float>& bornPos, MonsCreateParam &createParam, Point3<float>& dir, int32_t monsIdx = -1, MonsterExtendInfo * pExtend = NULL, uint32_t groupId = 0);
	
	//路径和巡逻类型
	virtual void SetAiPath(uint32_t pathId, int8_t patroType);
	//编队
	virtual int32_t GetFormationId() { return m_formationId; };
	virtual void SetFormationId(int32_t fromationId) { m_formationId = fromationId; }

	//获取对应部件指针,part dynamic外部对应转一下
	virtual Part* GetPart(uint32_t partType);

	//scenceId:目标场景ID（唯一ID，静态地图场景ID和地图ID相同），dstPos：目标场景坐标，mapId：地图ID,transParam:传送参数
	virtual bool TransScene(uint32_t scenceId, Point3<float>& dstPos, uint32_t mapId, STransParam &transParam);
	virtual bool MoveTo(Point3<float> dstPos);					//移动
	virtual void StopMove(bool isBroadClent=true);									//停止当前移动,广播告诉其他人
	//virtual void StopMove()	 { StopMove(true); }
	
	//瞬间移动
	virtual bool Teleporting(Point3<float> dstPos);
	//被击退
	virtual bool BeatBack(Point3<float> dstPos);

	//视野裁剪接口 后面需要做更复杂的视野裁剪那么子类继承去实现
	virtual bool ViewFliter(Creature* pCreature, float dict);

	void OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen);
	virtual void OnTimer(uint32_t nTimerID);

	//virtual bool AddStateBag(uint32_t skillid, uint32_t lev, ProtoBuf::BuffBagDetailInfo* pClientStatebag, Creature* pTarget = nullptr);

	void AddAttackMonsterTarget(float dict,Creature * pTarget);
public:
	const MonsterMonsterCfgInfo* GetMonsterInfo() const { return m_monsterCfgInfo; }//获得配置表中的信息
	Point3<float> GetBornPos() const { return m_bornPos; }
	void OnEnterDropTime(uint64_t dropOnwerID);
	bool IsCanBoPi();
	uint64_t GetKillerID();
public:
	//PVP 相关
	virtual bool IsFriendSide(Creature* pCreature);
public:
	//视野类接口
	virtual void FindCreatureInScene(Uint64Lst& clist);
	virtual void AddSeeNewCreature(Creature* pCreature);
	//virtual void AddBeenSeeLst(CharIDType cid);
	//virtual void DelBeenSeeLst(CharIDType cid);

	////////////////////////////////////////////////////////////////////////////////
	// lua class
	// lua 注册函数不支持有64位和枚举类型的的函数注册，需要重新包装为支持的类型
	// 返回值为bool类型的函数一律重新封装为返回值为int类型的lua函数
public:
	static bool RegistLuaClass(lua_State* state);
	int32_t LuaGetAttr(uint32_t CANum);

public:
	//判断生物身上是否有buff状态包
	virtual bool HasBuffStateBag(uint32_t stateBagId);
	virtual void BuffEffect_ReduceDamage(Creature* pAttack, uint32_t skillId, int32_t &damage);
	virtual void BuffEffect_AddExtraDamage(Creature* pBeAttacked, uint32_t skillId, int32_t &damage);
	virtual void BuffEffect_CleanupAllFadeOutEffects(BuffInfoEndType endType);

	//获取怪物模型半径
	virtual float GetModelRadius();
public:
	int GetMonsterFightType() const;
	MonsterType GetMonsterType() const;
	MonsterCfgType GetMonsterCfgType() const;
	MonsterViewType GetMonsterViewType() const;
	ParentMonsterType GetParentMonsterType() const;
	bool	IsMonsterTa() const; 
	void SetMonsterUnionID(uint32_t unionID);
	void SetMonsterMasterCID(CharIDType masterID);
	void SetMonsterMasterKind(int creatureKind);
	void SetMonstaeMasterName(std::string strName);
	void SetMonsterCampID(uint32_t campID);
	void SetMonsterTeamID(uint32_t teamID);
	//是否是专属怪
	bool IsOnlyOnwerOnePlayerMonster();
	//是否是自己的专属怪，或是属于自己队伍的专属怪
	bool IsOnlyMineMonster(CharIDType cid);
	uint32_t   GetMonsterUnionID();
	CharIDType GetMonsterMasterCID();
	int		   GetMonsterMasterKind();
	uint32_t   GetMonsterCampID();
	std::string GetMasterName();
	//怪物死亡归属列表，即杀死此怪的玩家列表,现有用于分配经验和怪物死亡计数
	//返回值返回所有归属列表
	//参数返回归属列表中在有组队的玩家
	std::vector<uint64_t> GetMonsterKillerList(std::map<uint32_t, std::vector<uint64_t> >* mapTeamPlayer = NULL);
public:
	uint8_t GetBoPiNum(){ return m_byteHaveBopiNum; }
	void SetBoPiNum(uint8_t num){ m_byteHaveBopiNum = num; }
	void SetModelScale(const Point3<float> & pos);//{ m_modelScale = pos; };
	bool UpdateDeadBeback();  //更新死亡击飞状态
	bool IsDeadBeback(){ return m_bIsDeadBeback; };
	//处理怪物死亡，出身时触发事件
	bool OnDoEffect(bool isBorn);
	void StopAI();
public:
	uint32_t GetMonsterGroupId() const { return m_monsterGroupId; }
	void SetMonsterGroupId(uint32_t groupId) { m_monsterGroupId = groupId; }
public:
	//virtual bool BeenSeeByPlayer();
	//根据配置的公式算出该怪物属性值
	uint64_t GetMonsterAttrFormula(VEC_INT32  &para);
public://获取怪物仇恨列表 60米以为的才生效
	void	GetHateListID(VEC_UINT64& hastIds);
protected:
	int64_t m_attrs64[M_ATTR_ULONG_END];
	int32_t m_attrs[M_MAX];
	Part* m_pPart[PART_MAX];
protected:
	Point3<float>		m_bornPos; //怪物出生位置，AI需要
	std::list<CharIDType>      m_beenSeePlayerlst; //被见的玩家Id
	int32_t  m_formationId;	//编队ID
	int32_t  m_monsIdx; //怪物索引，默认值为 -1
	uint8_t	 m_byteHaveBopiNum;  //剥皮次数
	uint64_t	m_dropOwnerID;	//怪物归属的玩家ID
	MonsterDropOwnerState m_dropState; //怪物掉落状态
	bool			m_bIsDeadBeback; //是否死亡击退
	MonsterExtendInfo m_stExtendInfo;
	uint64_t m_i64KillerID;			//击杀者ID
	MonsCreateParam m_createParam;	//怪物创建参数
protected:
	uint32_t m_monsterId;
	float m_viewAngle; //视野为扇形时，视野的角度
	const MonsterMonsterCfgInfo* m_monsterCfgInfo;
	uint64_t m_corpseTime; //怪物死亡后尸体到期时间
	uint64_t m_existTime; //怪物存在到期时间
	Point3<float> m_modelScale;
	bool			m_bIsBornFlag;  //是否是刚出身标记，客户端要用
	uint64_t m_aitimeafterborn;
	uint32_t m_monsterGroupId = 0;
};
